package thread;

public class testDemo15 {
    public static void main(String[] args) {
        Object A=new Object();//对象A
        Object B=new Object();//对象B
        //创建线程t1拿到锁
        Thread t1=new Thread(()->{
            synchronized (A) {
                System.out.println("线程t1拿到了锁A");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException();
                }//线程进入休眠，保证另一个线程也能够拿到锁

                //尝试拿到对方的锁,此时锁A没有释放
                synchronized (B) {
                    System.out.println("线程t1拿到了两把锁");
                }
            }
        });
        Thread t2=new Thread(()->{
            synchronized (A) {
                System.out.println("线程t2拿到了锁A");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException();
                }//线程进入休眠，保证另一个线程也能够拿到锁

                //尝试拿到对方的锁，没有释放自己的锁
                synchronized (B) {
                    System.out.println("线程t2拿到了两把锁");
                }
            }
        });
        //启动线程
        t1.start();
        t2.start();
    }//此时就会发生线程等待
}
